/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.DALProduct;
import DTO.DTOProduct;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Pham
 */
public class BLLProduct {
    DALProduct dalPro;
    public BLLProduct()
    {
        dalPro = new DALProduct();
    }
    
    //get products
    public ArrayList<DTOProduct> getProducts()
    {
        return dalPro.getProducts();
    }
    
    //search products
    public ArrayList<DTOProduct> searchProducts(String name, String manu, long from, long to)
    {
        return dalPro.searchProducts(name, manu, from, to);
    }
    
    //get all products with model
    public DefaultTableModel getProductModel()
    {
        try
        {
            DefaultTableModel model = new DefaultTableModel();
            String[] cols = {"Mã SP", "Tên SP", "Hãng SX", "Giá", "Số lượng còn", "Ngày cập nhật"};
            model.setColumnIdentifiers(cols);
            for(DTOProduct itemPro : dalPro.getProducts())
            {
                model.addRow(new Object[]{itemPro.getProductID(), itemPro.getProductName(), itemPro.getManu().getManuName(),
                    itemPro.getProductPrice(), itemPro.getProductQuantity(), itemPro.getProductDateUpdate()});
            }
            return model;
        }
        catch(Exception ex)
        {
            System.err.printf(ex.getMessage());
            return null;
        }
    }
    
    //method search product return model
    public DefaultTableModel searchProductModel(String name, String manu, long priceFrom, long priceTo)
    {
        try
        {
            DefaultTableModel model = new DefaultTableModel();
            String[] cols = {"Mã SP", "Tên SP", "Hãng SX", "Giá", "Số lượng còn", "Ngày cập nhật"};
            model.setColumnIdentifiers(cols);
            for(DTOProduct itemPro : dalPro.searchProducts(name, manu, priceFrom, priceTo))
            {
                model.addRow(new Object[]{itemPro.getProductID(), itemPro.getProductName(), itemPro.getManu().getManuName(),
                    itemPro.getProductPrice(), itemPro.getProductQuantity(), itemPro.getProductDateUpdate()});
            }
            return model;
        }
        catch(Exception ex)
        {
            System.err.printf(ex.getMessage());
            return null;
        }
    }
    
    //method get Product by ID
    public DTOProduct getProductbyID(int Id)
    {
        if(dalPro.getProductbyID(Id) != null)
            return dalPro.getProductbyID(Id);
        return null;
    }
    
    //method insert new row to table product
    public boolean insertRowProduct(DTOProduct dtoPro)
    {
        return dalPro.insertRowProduct(dtoPro);
    }
    
    //method update row Product
    public boolean updateRowProduct(DTOProduct dtoPro)
    {
        return dalPro.updateProduct(dtoPro);
    }
    
    //method delete row product
    public boolean deleteRowProduct(DTOProduct dtoPro)
    {
        return dalPro.deleteProduct(dtoPro);
    }
    
    //method write image to directory
    public boolean writeImage(File file, String savePath)
    {
        try{
            BufferedImage buff = ImageIO.read(file);
            File checkExits = new File(savePath);
            if(!checkExits.exists()){
                if(ImageIO.write(buff, "png", checkExits))
                    return true;
            }
            return false;
        }
        catch(IOException ex)
        {
            System.err.printf(ex.getMessage());
            return false;
        }
    }
    
    //method delete image when delete product or change image
    public boolean deleteImage(String savePath)
    {
        try{
            File file = new File(savePath);
            if(file.exists()){
                return file.delete();
            }
            return false;
        }
        catch(Exception ex)
        {
            System.err.printf(ex.getMessage());
            return false;
        }
    }
    
    //method search product to import
    public DefaultTableModel searchProducttoImport(String name, String Manu, int quantityFrom, int quantityTo)
    {
        try
        {
            DefaultTableModel model = new DefaultTableModel();
            String[] cols = {"Mã SP", "Tên SP", "Hãng SX", "Số lượng"};
            model.setColumnIdentifiers(cols);
            for(DTOProduct itemPro : dalPro.searchProductToImport(name, Manu, quantityFrom, quantityTo)){
                model.addRow(new Object[]{itemPro.getProductID(), itemPro.getProductName(),
                                        itemPro.getManu().getManuName(), itemPro.getProductQuantity()});
            }
            return model;
        }
        catch(Exception ex)
        {
            System.err.printf(ex.getMessage());
            return null;
        }
    }
    
    //public get products to import
    public DefaultTableModel getProducttoImport()
    {
        try
        {
            DefaultTableModel model = new DefaultTableModel();
            String[] cols = {"Mã SP", "Tên SP", "Hãng SX", "Số lượng"};
            model.setColumnIdentifiers(cols);
            for(DTOProduct itemPro : dalPro.getProducts())
            {
                model.addRow(new Object[]{itemPro.getProductID(), itemPro.getProductName(),
                    itemPro.getManu().getManuName(), itemPro.getProductQuantity()});
            }
            return model;
        }
        catch(Exception ex)
        {
            System.err.printf(ex.getMessage());
            return null;
        }
    }
}
